<ui:composition template="template.xhtml"
	xmlns="http://www.w3.org/1999/xhtml"
	xmlns:c="http://java.sun.com/jsp/jstl/core"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:ui="http://java.sun.com/jsf/facelets">

	<ui:define name="head">
		<script type="text/javascript" src="wizard/wizard.nocache.js" />
		<c:if test="#{user.configurationEditor}">
			<script type="text/javascript">
			//<![CDATA[
			    function confirmRemoveDatastore(datastore) {
					$("#dsName").val(datastore);
					document.getElementById("deleteDatastore").style.display = "inline";
					document.getElementById("h4Id").innerText= '确定要删除"' + datastore + '"?\n如果删除该数据存储可能会导致相关联的任务失败!';
					$('#deleteDatastore').modal("show");
			    	// if (confirm('您是否确定删除"' + datastore + '"?\n如果任务依赖于数据存储，则重新删除数据存储可能会导致失败')) {
			    	// 	return true;
			    	// }
			    	return false;
			    }

			    var datacleaner = {
			    	onApiInitialized : function() {
			    		$(document).ready(function() {
					    	// check if wizard needs to start immediately检查向导是否需要立即启动
					    	var hashPartOfUri = window.location.hash;
					    	if (hashPartOfUri == "#startWizard") {
					    		datacleaner.startDatastoreWizard();
					    		window.location.hash = "";
					    	}
					    });
			    	}
			    }

			$(function(){
				$('#okButton').click(function(){
				$.ajax({
					type: "POST",
					url: "repository/"+$("#tenantContext").val()+"/datastores/"+$("#dsName").val()+".remove",
					data: {},
					dataType: "json",
					success: function(data){
						$("#deleteDatastore").modal("hide");
						window.location.reload();
					}
				});
				});
			});
			//]]>
			</script>
		</c:if>
	</ui:define>

	<ui:define name="content">

		<div id="DatastoresPage" class="container">
			<div class="modal fade bs-example-modal-lg" id="deleteDatastore" tabindex="-1" role="dialog"
				 aria-labelledby="myModalLabel">
				<div class="modal-dialog" role="document">
					<div class="modal-content">
						<div class="modal-header">
							<h4 id="h4Id" class="modal-title" align="center" style="font-size: 20px;color: #16c2f4"></h4>
						</div>
						<div class="modal-footer">
							<button id="okButton" type="button" class="btn btn-primary" >确定</button>
							<button id="noButton" type="button" class="btn btn-default" data-dismiss="modal">取消</button>
						</div>
					</div>
				</div>
			</div>
			<c:if test="#{user.configurationEditor}">
				<div>
					<button class="btn btn-primary" onclick="datacleaner.startDatastoreWizard();">
						<span class="glyphicon glyphicon-plus" aria-hidden="true" /> <span>创建数据源</span>
					</button>
				</div>
				<div class="spacer">用于创建数据源链接信息，可创建常见关系型数据库、CSV文件、Excel文件、xml文件、ElasticSearch存储等</div>
			</c:if>

			<div id="RootPanelTarget"></div>

			<div class="datastoreList">
				<ui:repeat var="ds" value="#{jsfHelper.datastores}">
					<input id="tenantContext" type="hidden" value="#{tenantContext.tenantId}"/>
					<input id="dsName" type="hidden" />
					<div
						class="datastore #{ds.fileDatastore ? 'FileDatastore' : ''} #{ds.simpleClassName}">
						<div class="btn-group right" role="group">
							<h:panelGroup rendered="#{user.configurationEditor}">
								<form style="display: inline;"
									action="repository/#{tenantContext.tenantId}/datastores/#{ds.name}.remove"
									method="post">
									<button class="btn btn-danger btn-sm"
										style="margin-left: 0.3em"
										onclick="return confirmRemoveDatastore('#{ds.name}');">
										<span class="glyphicon glyphicon-minus" aria-hidden="true" />
										<span>移除</span>
									</button>
								</form>
							</h:panelGroup>
							<h:panelGroup
								rendered="#{ds.fileDatastore and user.configurationEditor}">
								<button
									onclick="window.location='repository/#{tenantContext.tenantId}/datastores/#{ds.name}.download'"
									class="btn btn-default btn-sm">
									<span class="glyphicon glyphicon-download" aria-hidden="true" />
									<span>下载</span>
								</button>
							</h:panelGroup>
							<h:panelGroup
								rendered="#{ds.fileDatastore and user.configurationEditor}">
								<button
								    onclick="datacleaner.startDatastoreWizard();"
									class="btn btn-default btn-sm">
									<span class="glyphicon glyphicon-edit" aria-hidden="true" />
									<span>编辑</span>
								</button>
							</h:panelGroup>
							<h:panelGroup rendered="#{user.jobEditor and monitorUIPreferences.showWebstartLaunchOptions}">
								<button
									onclick="window.location='repository/#{tenantContext.tenantId}/datastores/#{ds.name}.analyze.jnlp'"
									class="btn btn-default btn-sm">
									<span class="glyphicon glyphicon-stats" aria-hidden="true" />
									<span>分析</span>
								</button>
							</h:panelGroup>
							<h:panelGroup rendered="#{user.queryAllowed}">
								<c:choose>
									<c:when test="#{param['gwt.codesvr'] != null}">
										<button
											onclick="window.open('query?ds=#{ds.name}&amp;gwt.codesvr=#{param[\'gwt.codesvr\']}','_blank','location=no,width=770,height=400,toolbar=no,menubar=no');"
											class="btn btn-default btn-sm">
											<span class="glyphicon glyphicon-console" aria-hidden="true" />
											<span>查询</span>
										</button>
									</c:when>
									<c:otherwise>
										<button
											onclick="window.open('query?ds=#{ds.name}','_blank','location=no,width=770,height=400,toolbar=no,menubar=no');"
											class="btn btn-default btn-sm">
											<span class="glyphicon glyphicon-console" aria-hidden="true" />
											<span>查询</span>
										</button>
									</c:otherwise>
								</c:choose>
							</h:panelGroup>
						</div>

						<h2 class="name">#{ds.name}</h2>

						<div class="panel panel-default">
							<div class="panel-heading">#{ds.description}</div>
							<div class="panel-body">
								<div class="row">
									<div class="col-xs-3">Type:</div>
									<div class="col-xs-9">#{ds.simpleClassName}</div>
								</div>
								<h:panelGroup
									rendered="#{ds.fileDatastore and user.configurationEditor}">
									<div class="row">
										<div class="col-xs-3">Filename:</div>
										<div class="col-xs-9">
											<span>#{ds.filename}</span>
											<h:panelGroup rendered="#{not ds.fileFound}">
												<span class="label label-danger">File not found</span>
											</h:panelGroup>
										</div>
									</div>
								</h:panelGroup>
								<h:panelGroup rendered="#{ds.hostnameBasedDatastore}">
									<div class="row">
										<div class="col-xs-3">Hostname:</div>
										<div class="col-xs-9">#{ds.hostname}</div>
									</div>
								</h:panelGroup>
								<h:panelGroup
									rendered="#{ds.jdbcDatastore and user.configurationEditor}">
									<div class="row">
										<div class="col-xs-3">URL:</div>
										<div class="col-xs-9">#{ds.jdbcUrl}</div>
									</div>
								</h:panelGroup>
								<h:panelGroup rendered="#{ds.usernameDatastore}">
									<div class="row">
										<div class="col-xs-3">Username:</div>
										<div class="col-xs-9">#{ds.username}</div>
									</div>
								</h:panelGroup>
								<h:panelGroup rendered="#{ds.compositeDatastore}">
									<div class="row">
										<div class="col-xs-3">Child datastores:</div>
										<div class="col-xs-9">#{ds.childDatastores}</div>
									</div>
								</h:panelGroup>
							</div>
						</div>
					</div>
				</ui:repeat>
			</div>

		</div>
	</ui:define>

</ui:composition>
